/*
 * ConfIdioma.java
 *
 * Created on 10 de diciembre de 2008, 20:18
 */
package Vistas;

import Dominio.CtrlDominio;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author  Juan Manuel Fuentes Diaz
 */
public class ConfIdioma extends javax.swing.JFrame implements Runnable {

    private CtrlDominio CD = CtrlDominio.getInstance();
    private String fs = System.getProperty("file.separator");
    /** Creates new form ConfIdioma */
    public ConfIdioma() {
        initComponents();
        group.add(jRadioButton1);
        group.add(jRadioButton2);
        group.add(jRadioButton3);
        this.setLocationRelativeTo(null);
        this.setTitle("Configurar Idiomas");
        jTextField1.setText("");
        jTextField2.setText("");
        jLabel7.setVisible(false);
        jLabel8.setVisible(false);
        jLabel9.setVisible(false);
        ImageIcon ic1=new ImageIcon(System.getProperty("user.dir")+fs+"src"+fs+"Image"+fs+"open.png");
        ImageIcon ic2=new ImageIcon(System.getProperty("user.dir")+fs+"src"+fs+"Image"+fs+"clean.png");
        ImageIcon ic3=new ImageIcon(System.getProperty("user.dir")+fs+"src"+fs+"Image"+fs+"return.png");
        ImageIcon ic4=new ImageIcon(System.getProperty("user.dir")+fs+"src"+fs+"Image"+fs+"db_add.png");
        jButton1.setIcon(ic1);
        jButton2.setIcon(ic1);
        jButton3.setIcon(ic4);
        jButton4.setIcon(ic3);
        jLabel8.setIcon(ic2);
        jLabel9.setIcon(ic2);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        group = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Lucida Sans", 1, 14));
        jLabel1.setForeground(new java.awt.Color(0, 51, 153));
        jLabel1.setText("CONFIGURAR IDIOMAS");

        jRadioButton1.setText("Castellano");

        jRadioButton2.setText("Catalan");

        jRadioButton3.setText("Ingles");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("Elige un idioma para crear los diccionarios a partir de archivos *.txt:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 2, 11));
        jLabel3.setText("(Recuerda que estos archivos contienen en la primera linea un entero con el ");

        jLabel4.setFont(new java.awt.Font("Tahoma", 2, 11));
        jLabel4.setText(" total de palabras de diccionario y a partir de la segunda linea cada palabra)");

        jLabel5.setText("Elige el *.txt que contenga el diccionario de palabras funcionales:");

        jLabel6.setText("Elige el *.txt que contenga el diccionario de palabras NO funcionales:");

        jButton1.setText("Abrir");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("Abrir");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setText("CREAR DICCIONARIOS");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton4.setText("Volver");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 433, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addComponent(jLabel3)
                .addGap(61, 61, 61))
            .addGroup(layout.createSequentialGroup()
                .addGap(89, 89, 89)
                .addComponent(jRadioButton2)
                .addGap(68, 68, 68)
                .addComponent(jRadioButton1)
                .addGap(83, 83, 83)
                .addComponent(jRadioButton3)
                .addContainerGap(103, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(177, 177, 177)
                .addComponent(jLabel1)
                .addContainerGap(188, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel2)
                .addContainerGap(35, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 460, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel1)
                .addGap(30, 30, 30)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton2)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton3))
                .addGap(28, 28, 28)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        javax.swing.JFileChooser filechooser = new javax.swing.JFileChooser();
        int returnVal = filechooser.showOpenDialog(jButton1);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = filechooser.getSelectedFile();
            if (file.getName().substring(file.getName().length() - 4, file.getName().length()).compareTo(".txt") == 0) {
                jTextField1.setText(file.getAbsolutePath());
            } else {
                JOptionPane.showMessageDialog(null, "Archivo no valido");
            }
        }        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        javax.swing.JFileChooser filechooser = new javax.swing.JFileChooser();
        int returnVal = filechooser.showOpenDialog(jButton2);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = filechooser.getSelectedFile();
            if (file.getName().substring(file.getName().length() - 4, file.getName().length()).compareTo(".txt") == 0) {
                jTextField2.setText(file.getAbsolutePath());
            } else {
                JOptionPane.showMessageDialog(null, "Archivo no valido");
            }
        }        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2MouseClicked

    private String cualSelected() {
        if (jRadioButton1.isSelected()) {
            return jRadioButton1.getText();
        } else if (jRadioButton2.isSelected()) {
            return jRadioButton2.getText();
        } else {
            return jRadioButton3.getText();
        }
    }

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        if (jRadioButton1.isSelected() || jRadioButton2.isSelected() || jRadioButton3.isSelected()) {
            if (jTextField1.getText().compareTo("") != 0 && jTextField2.getText().compareTo("") != 0) {
                String idioma = cualSelected();
                String dp = jTextField1.getText();
                String dpn = jTextField2.getText();
                CargarDicc cadi;
                jLabel7.setText("Cargando diccionarios...esta operaci�n puede tardar unos segundos");
                if (idioma.compareTo("Castellano") == 0) {
                    cadi = new CargarDicc(1, 1, dp);
                    cadi.start();
                    jLabel7.setVisible(true);
                    JOptionPane.showMessageDialog(null,"Cargando diccionarios...esta operaci�n puede tardar unos segundos");
                    while (cadi.isAlive()) {}
                    jLabel8.setVisible(true);
                    cadi = new CargarDicc(1, 2, dpn);
                    cadi.start();
                    while (cadi.isAlive()) {}
                    jLabel7.setVisible(false);
                    jLabel9.setVisible(true);
                } else if (idioma.compareTo("Catalan") == 0) {
                    cadi = new CargarDicc(2, 1, dp);
                    cadi.start();
                    jLabel7.setVisible(true);
                    JOptionPane.showMessageDialog(null,"Cargando diccionarios...esta operaci\u00f2n puede tardar unos segundos");
                    while (cadi.isAlive()) {}
                    jLabel8.setVisible(true);
                    cadi = new CargarDicc(2, 2, dpn);
                    cadi.start();
                    while (cadi.isAlive()) {}
                    jLabel7.setVisible(false);
                    jLabel9.setVisible(true);
                } else {
                    cadi = new CargarDicc(3, 1, dp);
                    cadi.start();
                    jLabel7.setVisible(true);
                    JOptionPane.showMessageDialog(null,"Cargando diccionarios...esta operaci\u00ef2n puede tardar unos segundos");
                    jLabel8.setVisible(true);
                    cadi = new CargarDicc(3, 2, dpn);
                    cadi.start();
                    while (cadi.isAlive()) {}
                    jLabel7.setVisible(false);
                    jLabel9.setVisible(true);
                }

                int o = JOptionPane.showConfirmDialog(null, "Diccionario creado, \u00bfdeseas crear otro diccionario?", "Diccionario cargado",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (o == 0) {
                    jTextField1.setText("");
                    jTextField2.setText("");
                    jRadioButton1.setSelected(false);
                    jRadioButton2.setSelected(false);
                    jRadioButton3.setSelected(false);
                    jLabel9.setVisible(false);
                    jLabel8.setVisible(false);
                } else {
                    this.dispose();
                    new Idioma().setVisible(true);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Selecciona los archivos *.txt");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Selecciona un idioma");
        }        
    }//GEN-LAST:event_jButton3MouseClicked

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked
        this.dispose();
        new Idioma().setVisible(true);
    // TODO add your handling code here:
    }//GEN-LAST:event_jButton4MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ConfIdioma().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup group;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
    public void run() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
